<?php

class User_LoginController extends Zend_Controller_Action
{

    public function init()
    {
        //$this->_helper->layout->disableLayout();
        //$this->_helper->viewRenderer->setNoRender(true);   
                
    }

    public function indexAction()
    {
        // action body
        exit;
    }
    
    // LOGIN DE USUARI
    public function putAction(){
        
        $mail = $this->_getParam('id');
        $pass = $this->_getParam('password');
        
        if(is_null($mail) || is_null($pass)){
            $res = -1;
        } else {
            $model_user = new User_Model_Users();
            $res = $model_user->login($mail, $pass);

            if($res == -1)      $res = -2;      // No existeix user
            else if($res == -2) $res = -3;      // Password invalid
            else {
                $model_auth = new User_Model_AuthUsers();
                $res = $model_auth->insert_token($mail);  
            }

        }
        echo Zend_Json_Encoder::encode($res);
      
        exit;
    }
    
    
    // CREAR NOU USUARI
    public function postAction(){


        $mail = $this->_getParam('mail',NULL);
        $pass = $this->_getParam('password',NULL);
        $nom = $this->_getParam('nom',NULL);
        
        if(is_null($mail) || is_null($pass) ||is_null($nom)){
            $res = -1;
        } else {
            $model = new User_Model_Users();
            $ret_model = $model->crea_compta($mail, $pass, $nom); 

            if($ret_model == -1)    $res = -2;
            else                    $res = 0;
            
        }

        // curl -X POST -d "mail=abc@gmail.com&password=12345&nom=Joan" "server_mytaxi_move.local/user/login/"

        echo Zend_Json_Encoder::encode($res);
        exit; 

    }

}
?>
